{% extends 'layout/basic.html' %}
{% load static %}
{% block title %}
    用户登录
{% endblock %}
{% block css %}
    <style>
        .account {
            display: flex;
            justify-content: center;
        }

        h1 {
            text-align: center;
        }

        .error-msg {
            color: #1b6d85;
            position: absolute;
            font-size: 12px;
        }

        .form {
            background-image: url({% static 'image/img.png' %});
            background-size: 100% 100%;
            background-attachment: fixed;
            position: fixed;
            top: 250px;
            margin: 0 auto;
            border: 1px solid #5e5e5e;
            padding: 20px;
            border-radius: 10px;
            transition: transform 0.3s cubic-bezier(0.165, 0.84, 0.44, 1) !important;
            transform-style: preserve-3d !important;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.2) !important;
        }

        .form:hover {
            box-shadow: 0 0 25px rgba(0, 0, 0, 0.4) !important;
        }
        body {
            background-image: url({% static 'image/img10.jpg' %});
            background-size: cover;
            background-attachment: fixed;
            background-position: 50% 50%;
            background-repeat: no-repeat;
        }
    </style>
{% endblock %}


{% block content %}
    <div class="account">
        <div class="form">
            <h1>用户短信登录</h1>
            <form id="smsForm" method="post" novalidate>
                {% csrf_token %}
                {% for field in form %}
                    {% if field.name == 'code' %}
                        <div class="form-group">
                            <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                            <div class="clearfix">
                                <div class="col-md-6" style="padding-left: 0;">
                                    {{ field }}
                                    <span class="error-msg"></span>
                                </div>
                                <div class="col-md-6" style="padding-left: 20px;"><input type="button" id="btnSms"
                                                                                         class="btn btn-default"
                                                                                         value="获取验证码">
                                </div>
                            </div>
                        </div>
                    {% else %}
                        <div class="form-group">
                            <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                            {{ field }}
                            <span class="error-msg"></span>
                        </div>
                    {% endif %}
                {% endfor %}
                <div>
                    <div style="float: right">
                        <a href="{% url 'login' %}">用户名密码登录</a>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-3">
                        <input id="btnSubmit" type="button" class="btn btn-primary" value="登  录"/>
                    </div>
                </div>
            </form>
        </div>
    </div>


{% endblock %}

{% block js %}
    <script>
        $(function () {
            bindClickBtnSms();
            bindClickSubmit();
        })

        /*
            点击验证码发送短信
         */
        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty()
                // 获取用户的手机号
                var mobile_phone = $('#id_mobile_phone').val();

                $.ajax({
                    url: "{% url 'send_sms'%}",
                    type: "GET",
                    data: {mobile_phone: mobile_phone, tpl: 'login'},
                    dataType: 'JSON',
                    success: function (res) {
                        //console.log(res)
                        if (res.status) {
                            // 启动倒计时功能
                            sendSmsRemind();
                        } else {
                            // 错误 显示对应的错误信息
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            });
                        }
                    }
                })
            })
        }

        /*
            倒计时功能
         */
        function sendSmsRemind() {
            var $smsBtn = $('#btnSms');
            // 禁用状态
            $smsBtn.prop('disabled', true)
            var time = 60;
            var remind = setInterval(function () {
                $smsBtn.val(time + '秒重新发送');
                time -= 1;
                if (time < 1) {
                    clearInterval(remind);
                    $smsBtn.val('点击获取验证码').prop('disabled', false);
                }
            }, 1000)
        }

        /*
            实现登录
         */
        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty();
                $.ajax({
                    url: '{% url 'login_sms' %}',
                    type: 'POST',
                    data: $('#smsForm').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            // 跳转到登录页面
                            location.href = res.data
                        } else {
                            // 错误 显示对应的错误信息
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            });
                        }
                    }
                });
            });
        }

    </script>
{% endblock %}


